let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page:1,
        currentTab:0,
        dotation:false,
        role:0,
        inputText:'',
        type_index:0,
        number:1
    
      
    },  

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        var currentTab = options.currentTab;
        if(currentTab != undefined){
          var role = options.role;
          this.setData({
            currentTab:currentTab,
            role:role
          })
        }
        //console.log(currentTab);
        that.setData({
            platform:app.globalData.platform, 
            
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.domain,
            uploadUrl:app.globalData.uploadUrl,
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    switchNav(event){
        var cur = event.currentTarget.dataset.current;
        console.log(cur);  
        //每个tab选项宽度占1/5
        var singleNavWidth = this.data.width / 5;
        //tab选项居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })      
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
            this.getList();
          
        }
    },
    switchTab(event){
        var cur = event.detail.current;
        var singleNavWidth = this.data.width / 5;
        var left = (cur - 2) * singleNavWidth;
        this.setData({
            currentTab: cur,
            navScrollLeft: left
        });
    },
    goBack(){
        console.log('go back')
        wx.navigateBack(-1)
      },
      goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;
        var parent_id=wx.getStorageSync('parent_id');
        if(url=="#"){
    
        }else{
            if(url=='index'){
                wx.reLaunch({
                  url: url,
                })
            }else{
                wx.navigateTo({
                    url: url,
                  })
            }
        
        }
        console.log(url);
    },
    showModals:function(msg,url,index){
      wx.showModal({
          title: '系统提示',
          content:msg,
          success:function(res){
            console.log(res)
             if(res.confirm){
              if(url){
                if(index == 'index'){
                  wx.reLaunch({
                    url: url,
                  })
                }else{
                  wx.navigateTo({
                    url: url,
                  })
                }
              }
             }
             if(res.cancel){

             }
          }
      })
  },
    onShow() {
        var open = wx.getStorageSync('open');
        this.setData({
            open:open
        })
        this.getIndex()
    },
    getIndex(){
       let that = this;
       let data = this.data;
       var open=wx.getStorageSync('open');       
      wx.request({
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: {
          user_id:open.id,
          page:data.page,
          role:data.role
         //phone:userInfo.phone                  
        },
        //method: 'POST',
        dataType: 'json',
        timeout: 30000,
        url: app.globalData.domain + 'wechat/open/getIndex',
        success: (res) => {
    
        let data = res.data;
        let type = data.type;
        var type_array = [];
        var types =[];
        type_array.push('请选择洗车套餐类型');
        types.push('');
        type.forEach(ele=>{
          type_array.push(ele.name+' ￥'+ele.price+'/次');
          types.push(ele);
        })
        console.log(data);
          that.setData({
            list:data.list,
            navData:data.navData,
            types:types,
            type_array:type_array,
            type:type    
   
          })
        }
      
       })
    },
    call(e){
        console.log(e)
        var phone =e.currentTarget.dataset.phone;
        wx.makePhoneCall({
          phoneNumber: phone,
        })
    },
    getList(){
        let that = this;
        let data = this.data;
        var open=wx.getStorageSync('open');
        var currentTab = data.currentTab;
       console.log(currentTab);
       var navData = data.navData;
       console.log(navData);
      console.log('currentTab'+currentTab);
      var role = navData[currentTab].role;
      console.log(role);
       wx.request({
         headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
         data: {
           user_id:open.id,
           page:1,
           role:role
          //phone:userInfo.phone                  
         },
         //method: 'POST',
         dataType: 'json',
         timeout: 30000,
         url: app.globalData.domain + 'wechat/open/getList',
         success: (res) => {     
         let data = res.data;
         console.log(data);
        that.setData({
             list:data.list,
             count:data.count,
           //  navData:data.navData,     
            })
         }
       
        })
    },
    inputText(e){
        var value = e.detail.value;
        this.setData({
            inputText:value
        })
    },
    toSearch(e){
        console.log(e);
        var inputText = this.data.inputText;
        let that = this;
        wx.request({
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            data: {
             inputText:inputText
             //phone:userInfo.phone                  
            },
            //method: 'POST',
            dataType: 'json',
            timeout: 30000,
            url: app.globalData.domain + 'wechat/open/toSearch',
            success: (res) => {     
            let data = res.data;
            console.log(data);
           that.setData({
                list:data.list,
                count:data.count,
                
              //  navData:data.navData,     
               })
            }
          
           })
        
    },
    updateUserRole(e){
        var id = e.currentTarget.dataset.id;
        var msg = e.currentTarget.dataset.msg;
        var role = e.currentTarget.dataset.role;
        var open = wx.getStorageSync('open');
        let that = this;
        wx.showModal({
          title: '系统提示',
          content: msg,
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              wx.request({
                url: app.globalData.domain+'wechat/open/updateRole',
                data:{from:1,open_id:open.id,id:id,role:role},
                success:res=>{
                  var data=res.data;
                  console.log(data);
                  if(data.code==0){
                    that.showModals(data.msg,data.url,data.index);
                   // that.getList();
                  }else{
                    that.showModals(data.msg,data.url,data.index);
                  }
                  
                }
              })
            }
          }
        })
        
      },
    call(e){
      console.log(e);
      var phone = e.currentTarget.dataset.phone;
      wx.makePhoneCall({
        phoneNumber: phone,
      })
    },
    typeChange(e){
      var index = e.detail.value;
      this.setData({
        type_index:index
      })
    },
    hideAdd(w){
      this.setData({
        use:0
      })
    },
    addCard(e){
      console.log(e)
      var id = e.currentTarget.dataset.id;
      var mobile = e.currentTarget.dataset.mobile;
     if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(mobile))) {
       this.showModals('客户手机号码不正确，请完善后再增加','#','#');
        return;
      }
      this.setData({
        id:id,
        mobile:mobile,
        use:1
      })
    },
    inputNumber(e){
      var number = e.detail.value;
      this.setData({
        number:number
      })
    },
    toAdd(){
      let that = this;
      let data = this.data;
      var type_index = data.type_index;
      var types = data.types;
      var title = types[type_index].name;
      var type_id = types[type_index].id;
      var mobile = data.mobile;
      var id = data.id;
      var number = data.number;
      var open = wx.getStorageSync('open');
      if(type_index == 0){
        that.showModals('请选择洗车套餐类型','#','#');
        return;
      }else{
         wx.showModal({
           title: '系统提醒',
           content: '确定为客户 [ '+mobile+' ] 增加 '+number+' 次 [ '+title+' ]套餐吗?',
           complete: (res) => {
             if (res.cancel) {
               
             }
         
             if (res.confirm) {
               wx.request({
                url: app.globalData.domain+'wechat/open/addCard',
                data:{from:1,open_id:open.id,id:id,number:number,type_id:type_id},
                success:res=>{
                  var data=res.data;
                  if(res.statusCode==200){
                    that.showModals(data.msg,'#','#');
                    this.setData({
                      use:0
                    })
                  }else{
                    that.showModals('系统错误','#','#');
                  }
                }
               })
             }
           }
         })
      }

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})